--[[--------------------------------------------------
OpenFindFiles.lua
Author: mozers™
Version: 1.4.1
------------------------------------------------------
When the command "Find in files ..."
creating context menu item console - "Open files found"
------------------------------------------------------
connection:
In SciTEStartup.lua file add the line:
  dofile (props["SciteDefaultHome"].."\\tools\\OpenFindFiles.lua")
--]]--------------------------------------------------

local user_outputcontext_menu           -- original context menu of the console
local outputcontextmenu_changed = false -- sign modify the context menu
local command_num                       -- command number "OpenFindFiles" the menu Tools
local IDM_TOOLS = 9000
require 'shell'

--------------------------------------------------
-- Search Unemployed menu Tools
local function GetFreeCommandNumber()
	for i = 20, 299 do
		if props["command."..i..".*"] == "" then return i end
	end
end

--------------------------------------------------
-- Building a team to the Tools menu, and paste it in the context menu of the console
local function CreateMenu()
	local command_name = scite.GetTranslation("Open Find Files")
	command_num = GetFreeCommandNumber()

	-- item in the context menu of the console
	user_outputcontext_menu = props["user.outputcontext.menu.*"]
	props["user.outputcontext.menu.*"] = command_name.."|"..(IDM_TOOLS+command_num).."|||"..user_outputcontext_menu
	outputcontextmenu_changed = true

	-- command in the Tools menu
	props["command."..command_num..".*"] = "OpenFindFiles"
	props["command.mode."..command_num..".*"] = "subsystem:lua,savebefore:no,clearbefore:no"

end

--------------------------------------------------
-- Deleting a command from the Tools menu and restore the original context menu of the console
local function RemoveMenu()
	props["user.outputcontext.menu.*"] = user_outputcontext_menu
	outputcontextmenu_changed = false
	props["command."..command_num..".*"] = ""
end

--------------------------------------------------
-- The opening of the files listed in the console
function OpenFindFiles()
	local output_text = output:GetText()
	local cp = output:codepage()
	local str, path = output_text:match('"(.-)" in "(.-)"')
	path = path:match('^.+\\')
	local filename_prev = ''
	for filename, lineno in output_text:gmatch('([^\r\n:]+):(%d+):[^\r\n]+') do
		filename = filename:gsub('^%.\\', path)
		if filename ~= filename_prev then
			scite.Open(filename:to_utf8(cp))
			local pos = editor:findtext(str)
			if pos then editor:GotoPos(pos)
			else editor:GotoLine(lineno)
			end
			filename_prev = filename
		end
	end
	RemoveMenu()
end

--------------------------------------------------
AddEventHandler("OnMenuCommand", function(msg, source)
	if outputcontextmenu_changed then
		if msg ~= IDM_TOOLS+command_num and msg ~= IDM_FINDINFILES then RemoveMenu() end
	else
		if msg == IDM_FINDINFILES then CreateMenu() end
	end
end)
